<template>
  <el-dialog
    :title="!dataForm.actId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品id" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品规格" prop="goodsSize">
      <el-input v-model="dataForm.goodsSize" placeholder="商品规格"></el-input>
    </el-form-item>
    <el-form-item label="活动开始时间" prop="starttime">
      <el-input v-model="dataForm.starttime" placeholder="活动开始时间"></el-input>
    </el-form-item>
    <el-form-item label="活动结束时间" prop="endtime">
      <el-input v-model="dataForm.endtime" placeholder="活动结束时间"></el-input>
    </el-form-item>
    <el-form-item label="限购数量" prop="actNum">
      <el-input v-model="dataForm.actNum" placeholder="限购数量"></el-input>
    </el-form-item>
    <el-form-item label="商品原价" prop="goodsPrice">
      <el-input v-model="dataForm.goodsPrice" placeholder="商品原价"></el-input>
    </el-form-item>
    <el-form-item label="商品低价" prop="actPrice">
      <el-input v-model="dataForm.actPrice" placeholder="商品低价"></el-input>
    </el-form-item>
    <el-form-item label="砍价规则" prop="actRule">
      <el-input v-model="dataForm.actRule" placeholder="砍价规则"></el-input>
    </el-form-item>
    <el-form-item label="活动描述" prop="actContent">
      <el-input v-model="dataForm.actContent" placeholder="活动描述"></el-input>
    </el-form-item>
    <el-form-item label="库存" prop="actStock">
      <el-input v-model="dataForm.actStock" placeholder="库存"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          actId: 0,
          goodsId: '',
          goodsName: '',
          goodsSize: '',
          starttime: '',
          endtime: '',
          actNum: '',
          goodsPrice: '',
          actPrice: '',
          actRule: '',
          actContent: '',
          actStock: ''
        },
        dataRule: {
          goodsId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          goodsSize: [
            { required: true, message: '商品规格不能为空', trigger: 'blur' }
          ],
          starttime: [
            { required: true, message: '活动开始时间不能为空', trigger: 'blur' }
          ],
          endtime: [
            { required: true, message: '活动结束时间不能为空', trigger: 'blur' }
          ],
          actNum: [
            { required: true, message: '限购数量不能为空', trigger: 'blur' }
          ],
          goodsPrice: [
            { required: true, message: '商品原价不能为空', trigger: 'blur' }
          ],
          actPrice: [
            { required: true, message: '商品低价不能为空', trigger: 'blur' }
          ],
          actRule: [
            { required: true, message: '砍价规则不能为空', trigger: 'blur' }
          ],
          actContent: [
            { required: true, message: '活动描述不能为空', trigger: 'blur' }
          ],
          actStock: [
            { required: true, message: '库存不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.actId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.actId) {
            this.$http({
              url: this.$http.adornUrl(`/kanjia/act/info/${this.dataForm.actId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodsId = data.act.goodsId
                this.dataForm.goodsName = data.act.goodsName
                this.dataForm.goodsSize = data.act.goodsSize
                this.dataForm.starttime = data.act.starttime
                this.dataForm.endtime = data.act.endtime
                this.dataForm.actNum = data.act.actNum
                this.dataForm.goodsPrice = data.act.goodsPrice
                this.dataForm.actPrice = data.act.actPrice
                this.dataForm.actRule = data.act.actRule
                this.dataForm.actContent = data.act.actContent
                this.dataForm.actStock = data.act.actStock
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/kanjia/act/${!this.dataForm.actId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'actId': this.dataForm.actId || undefined,
                'goodsId': this.dataForm.goodsId,
                'goodsName': this.dataForm.goodsName,
                'goodsSize': this.dataForm.goodsSize,
                'starttime': this.dataForm.starttime,
                'endtime': this.dataForm.endtime,
                'actNum': this.dataForm.actNum,
                'goodsPrice': this.dataForm.goodsPrice,
                'actPrice': this.dataForm.actPrice,
                'actRule': this.dataForm.actRule,
                'actContent': this.dataForm.actContent,
                'actStock': this.dataForm.actStock
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
